package com.hc.model.code03.test1.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @version v1.0
 * @ClassName: ProxyPint
 * @Description: 代理对象工厂类
 * @Author: HC
 */
public class ProxyFactory {

    private TrainStation station = new TrainStation();

    public SellTickets getProxyObject() {
        SellTickets tickets = (SellTickets) Proxy.newProxyInstance(
                /*
                    ClassLoader loader  ：类加载器，用于加载代理类，可以通过目标对象获取类加载器
                    Class<?>[] interfaces ：代理实现的接口字节码对象
                    InvocationHandler h ：代理对象调用的处理程序
                 */
                station.getClass().getClassLoader(),
                station.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("代售点收取一些服务费用（JDK动态代理）");
                        Object obj = method.invoke(station, args);
                        return obj;
                    }
                }
        );
        return tickets;
    }
}
